export default class LoginService {
    static loginService=null;

    static getInstance(){
        if(this.loginService==null){
            this.loginService=new LoginService()
        }
        return this.loginService
    }

    login(params,router,alt,$http,$loading){
        $http.post("login",params.param1).then(msg=>{
            if(msg.data.status==="20000"){
                localStorage.setItem("token",msg.data.data);
                //配置全局token请求头
                $http.defaults.headers = {
                    'token': msg.data.data
                };
                this.upLoginNum(params.param,$http).then(() => {

                });
                this.querySystemUser(params.user,$http,router);
            }else{
                alt(msg.data.data,msg.data.message, {
                    confirmButtonText: '确定'
                    // callback: action => {
                    //     this.$message({
                    //         type: 'info',
                    //         message: `action: ${ action }`
                    //     });
                    // }
                });
                $loading().close()
            }
        })
    }

    upLoginNum(param,$http){
        return  $http.put("systemUser",param)
    }

    querySystemUser(param,$http,router){
        $http.get(`systemUser/${param}`).then(msg=>{
            localStorage["user"]=JSON.stringify(msg.data.data);
            router.push('/home/homePage');
            // const nickname = JSON.parse(localStorage["user"])[0];
            // console.log(nickname)
        })
    }

    onRegister(formData,$http,$message){
        $http.post("systemUser/add",formData).then(msg=>{
            if(msg.data.status==="50000"){
                $message({
                    type:"error",
                    message:"这个用户名太热门了，换一个吧~"
                })
            }else{
                $message({
                    type:"success",
                    message:"注册成功！"
                })
            }
        })
    }

    deAuthority($http){
        return $http.delete(`systemAuthority/delete`)
    }
}